﻿using System;
using Csla;
using BizControl.Library.BusinessObjects.People;
using BizControl.Library.BusinessObjects.BusinessRoles;
using BizControl.DalLinq;

namespace BizControl.Library.BusinessObjects.Participants
{
  [Serializable()]
  public class ParticipantInfo : ReadOnlyBase<ParticipantInfo>, ISupportEmptyValue
  {
    public Guid Id { get; private set; }
    public Guid ActivityId { get; private set; }
    public PersonInfo Person { get; private set; }
    public BusinessRoleInfo Role { get; private set; }

    internal ParticipantInfo(Guid id, Guid activityId, PersonInfo person, BusinessRoleInfo role)
    {
      Id = id;
      ActivityId = activityId;
      Person = person;
      Role = role;
    }

    internal ParticipantInfo(Participant participant)
    {
      if (participant != null)
      {
        Id = participant.ParticipantId;
        ActivityId = participant.Activity;
        Person = new PersonInfo(participant.PersonRef);
        Role = new BusinessRoleInfo(participant.BusinessRoleRef);
      }
      else
      {
        Person = new PersonInfo(PersonBO.GetEmptyPerson());
        Role = new BusinessRoleInfo(BusinessRoleBO.GetEmptyRole());
      }
    }

    public ParticipantInfo(ParticipantBO participant)
    {
      if (participant != null)
      {
        Id = participant.ParticipantId;
        ActivityId = participant.ActivityId ?? Guid.Empty;
        Person = new PersonInfo(participant.Person);
        Role = new BusinessRoleInfo(participant.Role);
      }
      else
      {
        Person = new PersonInfo(PersonBO.GetEmptyPerson());
        Role = new BusinessRoleInfo(BusinessRoleBO.GetEmptyRole());
      }
    }

    public ParticipantInfo(ParticipantInfo participant)
    {
      if (participant != null)
      {
        Id = participant.Id;
        ActivityId = participant.ActivityId;
        Person = new PersonInfo(participant.Person);
        Role = new BusinessRoleInfo(participant.Role);
      }
      else
      {
        Person = new PersonInfo(PersonBO.GetEmptyPerson());
        Role = new BusinessRoleInfo(BusinessRoleBO.GetEmptyRole());
      }
    }

    public override bool Equals(object obj)
    {
      if (obj is ParticipantInfo)
        return this.Id == (obj as ParticipantInfo).Id;
      return base.Equals(obj);
    }

    public override int GetHashCode()
    {
      return base.GetHashCode();
    }

    #region ISupportEmtpyValue Members

    public bool IsEmpty()
    {
      return Id == Guid.Empty;
    }

    #endregion


    public void UpdatePerson(PersonInfo newPersonSource)
    {
      Person = new PersonInfo(newPersonSource);
    }

    public void UpdateRole(BusinessRoleInfo newBusinessRoleSource)
    {
      Role = new BusinessRoleInfo(newBusinessRoleSource);
    }
  }
}
